package com.lz.football.module.friend
{
	import com.common.stdlib.ArrayUtil;
	import com.common.utils.Reflection;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.FriendEvent;
	import com.liangzhong.model.AppData;
	import com.lz.football.module.friend.item.FriendGrantItem;
	
	import common.BaseView.BaseView;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	
	import lz.controls.Alert;
	import lz.controls.nick.PagePanel;
	
	/**
	 *好友消息面板 (系统消息)
	 * @author Administrator
	 * 
	 */
	public class FriendMessageView extends BaseView
	{
		private var _vecItem:Vector.<FriendGrantItem>;
		/**
		 * 
		 */
		private var listContain:MovieClip;
		/**
		 * 
		 */
		private var _page:PagePanel;
		private var pageContain:MovieClip;
		/**
		 *全部接收 
		 */
		private var agreeBtn:SimpleButton;
		/**
		 *显示的信息  
		 */
		private var _showList:Array;
		public function FriendMessageView()
		{
			super();
		}
		override public function initUI(view:Object=null):void
		{
			symbolSkin = Reflection.createInstance("com.football.module.friend.sysView");
			this.addChild(symbolSkin);
			_windowBg = symbolSkin.windowBg;
			isDrag = true;
			setCloseBtn(symbolSkin.closeBtn);
			listContain = symbolSkin.listContain;
			pageContain = symbolSkin.pageContain;
			agreeBtn = symbolSkin.agreeBtn;
			_viewWidth = this.width;
			_viewHeight = this.height;
			initItem();
			addEvtListener();
		}
		private function initItem():void
		{
			_vecItem = new Vector.<FriendGrantItem>();
			var item:FriendGrantItem;
			for(var i:int = 0;i < 9;i++)
			{
				item = new FriendGrantItem();
				item.initUI();
				item.y = (item.height + 2) * i;
				item.type = 1;
				listContain.addChild(item);
				_vecItem.push(item);
			}
			_page = new PagePanel();
			_page.initUI("com.lz.commonUI.turnPage_white");
			pageContain.addChild(_page);
			_page.callBack = updataList;
		}
		override public function addEvtListener():void
		{
			super.addEvtListener();
			agreeBtn.addEventListener(MouseEvent.CLICK,onAgreehandler);
		}
		
		protected function onAgreehandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			
			var len:int = _showList.length;
			if(_showList == null)return;
			if(_showList.length <=0)
			{
				Alert.showErrorTip("没有邀请信息 ");
				return;
			}
			var arr:Array = [];
			for(var i:int = 0;i < len;i++)
			{
				arr.push(_showList[i].friendid);
			}
			AppEventDispatcher.getInstance().dispatchEvent(new FriendEvent(FriendEvent.AGGREE_INVITE,{arr:arr}));
		}
		/**
		 *更新列表 
		 * 
		 */
		public function updataList($str:String = ""):void
		{
			resetItem();
			_page.max = Math.ceil(AppData.getInstance().friendManager.inviteList.length/ 10);
			if(_page.min > _page.max)
				_page.min = _page.max;
			_showList = ArrayUtil.getPageSizeDatas(_page.min,10,AppData.getInstance().friendManager.inviteList);
			var len:int = _showList.length > _vecItem.length? _vecItem.length:_showList.length;
			for(var i:int = 0;i < len;i++)
			{
				_vecItem[i].data = _showList[i];
			}
		}
		/**
		 *重置item 
		 * 
		 */
		private function resetItem():void
		{
			var len:int  =_vecItem.length; 
			var item:FriendGrantItem;
			for each(item in _vecItem)
			{
				item.reset();
			}
		}
		override public function removeEvtListener():void
		{
			super.removeEvtListener();
			agreeBtn.removeEventListener(MouseEvent.CLICK,onAgreehandler);
		}
		override public function destroyView():void
		{
			removeEvtListener();
			while(_vecItem.length)
			{
				_vecItem.pop().destroyView();
			}
			_page.destroyView();
			_page = null;
			super.destroyView();
		}
	}
}